package service.msm.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.springframework.stereotype.Service;
import service.msm.service.MsmService;

import java.util.Map;

@Service
public class MsmServiceImpl implements MsmService {

    final String domain = "dysmsapi.aliyuncs.com";//短信API产品域名（接口地址固定，无需修改）
    final String accessKeyId = "LTAI4GKRg25GXxBVqfcKm31L";//accessKeyId
    final String accessKeySecret = "m7qbbWUPXbV5ma93aDGHEiwoAL0kYY";//accessKeySecret
    final String smsCode="SMS_202345036";//模板code
    final String signName="舰娘同人网站";//签名名称

    @Override
    public boolean send(Map<String, Object> param,  String phoneNum) {
        DefaultProfile profile = DefaultProfile.getProfile("default",accessKeyId ,accessKeySecret );
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysDomain(domain);
        request.setSysMethod(MethodType.POST);
        request.setSysAction("SendSms");
        request.setSysVersion("2017-05-25");

        request.putQueryParameter("PhoneNumbers", phoneNum);//手机号
        request.putQueryParameter("SignName",signName);//签名名称
        request.putQueryParameter("TemplateCode", smsCode);//模板code
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param));//随机验证码

        try {
            CommonResponse response = client.getCommonResponse(request);
            return response.getHttpResponse().isSuccess();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return false;
    }
}
